/**
 * 
 */
package com.xth8013.as.service.impl;

import com.xth8013.as.util.DictQueryHelper;
import com.xth8013.as.util.Message;
import com.xth8013.as.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xth8013.as.dao.DataDictionaryDao;
import com.xth8013.as.domain.DataDictionary;
import com.xth8013.as.exception.ServiceLogicException;
import com.xth8013.as.service.DataDictionaryService;
import com.xth8013.as.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 
 * @Description:
 * @author: tianhua.xie
 * @Date: 2017年2月9日 下午9:53:07 
 * 
 */
@Service("dataDictionaryService")
@Transactional
public class DataDictionaryServiceImpl implements DataDictionaryService {

	@Autowired
	private DataDictionaryDao dataDictionaryDao;

	@Override
	public void addDataItem(DataDictionary dataDictionary) {

		if (ObjectUtils.isBlank(dataDictionary)) {
			throw new ServiceLogicException("字典条目不能为空");
		}
		try {
			dataDictionaryDao.insert(dataDictionary);
		} catch (Exception e) {
			throw new ServiceLogicException(e);
		}
	}

	@Override
	public DataDictionary getDataItemById(Integer id) {

		if (ObjectUtils.isBlank(id)) {
			throw new ServiceLogicException("字典条目id不能为空");
		}

		return dataDictionaryDao.selectById(id);
	}

	@Override
	public void updateDataItem(DataDictionary dataDictionary) {

		if (ObjectUtils.isBlank(dataDictionary)) {
			throw new ServiceLogicException("字典条目不能为空");
		}
		try {
			dataDictionaryDao.update(dataDictionary);
		} catch (Exception e) {
			throw new ServiceLogicException(e);
		}
	}

    @Override
    public void deleteDataItemById(String decDictIds) {

        if (StringUtils.isBlank(decDictIds)) {
            throw new ServiceLogicException("字典条目id不能为空");
        }

        try {
            for (String id : decDictIds.split(",")) {
                dataDictionaryDao.deleteById(Integer.parseInt(id));
            }
        } catch (Exception e) {
            throw new ServiceLogicException(e);
        }
    }


    @Override
	public Map<String, Object> getDictListByHelper(DictQueryHelper helper) {

		Map<String,Object> result = new HashMap<String, Object>();

		if (StringUtils.isBlank(helper.getOrderField())) {
			helper.setOrderField("id");
		}

		helper.setTotalRecNum(dataDictionaryDao.cntDictByHelper(helper));

        List<DataDictionary> list = dataDictionaryDao.listDictByHelper(helper);
		result.put("total",helper.getRealyRecNum());
		result.put("rows",list);

		return result;
	}



}
